import pandas as pd
import mplfinance as mpf
import matplotlib.animation as animation

# 读取数据
idf = pd.read_csv('data/data1.csv', index_col=0, parse_dates=True)
# 按行切片2011-07-01 ~ 2011-12-30,截取所有的列
df = idf.loc['2011-07-01':'2011-12-30', :]
# 创建画布
fig = mpf.figure(style='charles', figsize=(7, 8))
# 第一张子图
ax1 = fig.add_subplot(2, 1, 1)
# 第二张子图
ax2 = fig.add_subplot(3, 1, 3)


# 绘制数据
def animate(ival):
    if (20 + ival) > len(df):
        print('no more data to plot')
        ani.event_source.interval *= 3
        if ani.event_source.interval > 12000:
            exit()
        return
    # 截取第0-20+ival行
    data = df.iloc[0:(20 + ival)]
    # 清除子图
    ax1.clear()
    ax2.clear()
    # 画图
    mpf.plot(data, ax=ax1, volume=ax2, type='candle')


# 动态渲染
ani = animation.FuncAnimation(fig, animate, interval=250)
# 展示图片
mpf.show()
